//
//  CommandThread.m
//  ServiceDebianManager
//
//  Created by Léo Lego on 11/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CommandThread.h"
#import "ServicesManager.h"
@implementation CommandThread
@synthesize command;
@synthesize value;
@synthesize passwd;
@synthesize host;
@synthesize port;

-(id)initWithMother:(ServicesManager *)Mother{
	if(self = [super init]){
		mother = Mother;
		command = [[NSString alloc]init];
		value = [[NSString alloc]init];
		passwd = [[NSString alloc]init];
		host = [[NSString alloc]init];
		port = [[NSNumber alloc]init];
		mySsh = [[ssh alloc] init];
	}
	return self;
}

-(void)setAllPasswd:(NSString*)pass Host:(NSString*)hst Port:(NSNumber*)prt Command:(NSString *) cmd{
	passwd = pass;
	host = hst;
	port = prt;
	command = cmd;
}

-(void)setAllCommand:(NSString *)cmd Value:(NSString*)vlu Passwd:(NSString*)pass Host:(NSString*)hst Port:(NSNumber*)prt{
	command = cmd;
	value = vlu;
	passwd = pass;
	host = hst;
	port = prt;
}
- (void)commandSystem{
	NSAlert * alert = [[NSAlert alloc] init];
	if([mother isNotActive])
	{
		[mother activeBar];
		
		int ret =[mySsh initWithHost:host port:port.intValue user:@"root" key:@"" keypub:@"" password:passwd];
		if(ret == -1){
			
			[alert setMessageText:@"Connection with server can't be etablished\nverify your server settings on application and ssh connection with the server"];
		} else if(ret == 1){	
			[alert setMessageText:@"Bad password !"];
			
		} else {
			[mySsh execCommand:command];
			NSString *show =[[NSString alloc] initWithFormat:@"Your server is %@ing",command];
			[alert setMessageText:show];
		}
		[mother unactiveBar];

	} else {
		[alert setMessageText:@"An ssh command is active ! \nif you things is longer, look your server settings, there is probably a problem."];
	}
	[alert runModal];

}
-(void)commandSimple{
	NSAlert * alert = [[NSAlert alloc] init];
	if([mother isNotActive])
	{
		[mother activeBar];
		int ret = [mySsh initWithHost:host port:port.intValue user:@"root" key:@"" keypub:@"" password:passwd];
		if(ret == -1){
			
			[alert setMessageText:@"Connection with server can't be etablished\nverify your server settings on application and ssh connection with the server"];
			
		} else if(ret == 1){
			[alert setMessageText:@"Bad password !"];
		} else if(ret == 0) {
			NSString *result = [mySsh execCommand:command];
			NSLog(@"return : %@",result);
			if(!result){
				[alert setMessageText:@"Your command is invalid or haven't return value!"];
			} else {	
				[alert setMessageText:[[NSString alloc] initWithFormat: @"Return Value : %@",result]];
			}
		}
		[mother unactiveBar];
	} else {
		[alert setMessageText:@"An ssh command is active ! \nif you things is longer, look your server settings, there is probably a problem."];
	}
	[alert runModal];
	
}

-(void)commandServices{
	NSAlert * alert = [[NSAlert alloc] init];
	if([mother isNotActive])
	{
		[mother activeBar];
		
		int ret = [mySsh  initWithHost:host port:port.intValue user:@"root" key:@"" keypub:@"" password:passwd];
		if(ret == -1){	
			[alert setMessageText:@"Connection with server can't be etablished\nverify your server settings on application and ssh connection with the server"];
			[alert runModal];
		} else if(ret == 1){
			[alert setMessageText:@"Bad password !"];
			[alert runModal];
		} else if(ret == 0) {
			NSMutableString * com = [[NSMutableString alloc] initWithString:@"service "];
			[com appendString:command];
			[com appendString:@" "];
			[com appendString:value];
			NSLog(@"CMD: %@",com);
			NSString *result = [mySsh execCommand: com];
			NSLog(@"Result : %@",com);
			NSLog(@"%@",result);
			
			/*if(!result){
			 [alert setMessageText:@"this service doesn't exist !"];
			 [alert runModal];
			 }*/
		}
		[mother unactiveBar];
	} else {
		[alert setMessageText:@"An ssh command is active ! \nif you things is longer, look your server settings, there is probably a problem."];
		[alert runModal];
	}

}

-(void)test{
	for (int i = 0; i != 10; ++i) {
		NSLog(@"[%d]%@:%d (%@) > service %@ %@ ",i,host,port.intValue,passwd,command,value);
		[NSThread sleepForTimeInterval:1];
	}

}


@end
